package com.magicode.cms.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.common.assist.json.JsonOperation;
import com.magicode.cms.dao.IFestivalDao;
import com.magicode.cms.entiy.base.Article;
import com.magicode.cms.entiy.base.EnterpriseFestivalRela;
import com.magicode.cms.entiy.base.Festival;
import com.magicode.cms.entiy.base.Picture;
import com.magicode.cms.service.IFestivalService;

@Service("servicefestival")
public class FestivalServiceImpl extends BaseServiceImpl<Festival, IFestivalDao> implements IFestivalService {

	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(json_key, "festivalDic");
		map.put(json_title, "影视节展名称");
		map.put("width", 130);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "year");
		map.put(json_title, "年份");
		map.put("width", 90);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "area");
		map.put(json_title, "地区");
		map.put("width", 130);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "subject");
		map.put(json_title, "主题");
		map.put("width", 80);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "beginTime");
		map.put(json_title, "开始时间");
		map.put("width", 130);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "endTime");
		map.put(json_title, "结束时间");
		map.put("width", 130);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "status");
		map.put(json_title, "状态");
		map.put("width", 70);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "valid");
		map.put(json_title, "有效性");
		map.put("width", 70);
		beaninfo.add(map);
		return beaninfo;
	}
	
	@Override
	public List<JsonOperation> getOpts(String url,String id) {
		List<JsonOperation> opts = new ArrayList<JsonOperation> ();
		JsonOperation opt= new JsonOperation();
		opt.setName("修改");
		opt.setUrl(getCtx().getContextPath()+"/"+url+"/getinfo/"+id+"/edit"+url);
		opt.setType("edit");
		opts.add(opt);
		opt= new JsonOperation();
		opt.setName("删除");
		opt.setUrl(getCtx().getContextPath()+"/"+url+"/"+id+"?_requestMethod=delete");
		opt.setType("delete2");
		opts.add(opt);
		opt= new JsonOperation();
		opt.setName("预览");
		opt.setUrl(getCtx().getContextPath()+"/p/festival/sec/" + id);
		opt.setType("preview");
		opts.add(opt);
		return opts;
	}

	@Override
	public String getHql(Map<String, String> querywhere) throws Exception {
		String hql =" from " +Festival.class.getName()+" "+entity_alias_name+" ";
		hql += " left join "+entity_alias_name+".festivalDic as fd ";
		hql += " left join "+entity_alias_name+".year as y ";
		hql += " left join "+entity_alias_name+".area as ar ";
		hql += " left join "+entity_alias_name+".subject as sub ";
		hql += " left join "+entity_alias_name+".organizer as org ";
		hql += " left join "+entity_alias_name+".mainPic as mp ";
		if(null!=querywhere && !querywhere.isEmpty()){
			hql += "where 1 = 1 ";
			if(querywhere.containsKey(field_id)){
				hql = hql+" and "+entity_alias_name+".id = '"+querywhere.get(field_id)+"'";
			}
			if(querywhere.containsKey("titleCn")){
				hql = hql+" and fd.titleCn like '%"+querywhere.get("titleCn").trim()+"%'";
			}
			if(querywhere.containsKey("yearId")){
				hql = hql+" and y.id = '"+querywhere.get("yearId")+"'";
			}
			if(querywhere.containsKey("areaId")){
				hql = hql+" and ar.id = '"+querywhere.get("areaId")+"'";
			}
			if(querywhere.containsKey("subjectId")){
				//节展主题
				hql = hql + " and sub.id = '"+querywhere.get("subjectId")+"' ";
			}
			if(querywhere.containsKey("statusId")){
				//节展状态
				hql = hql + " and " + entity_alias_name + ".status = "+querywhere.get("statusId")+" ";
			}
			if(querywhere.containsKey("validId")){
				//节展有效性
				hql = hql + " and " + entity_alias_name + ".valid = "+querywhere.get("validId")+" ";
			}
		}
		return hql;
	}

	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere, 20, field_grid_size);
			String hql = this.getHql(querywhere);
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			hql += "order by "+entity_alias_name+".createTime desc ";
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put("festivalDic", "fd.titleCn as festivalDic ");
			gridsel.put("year", "y.title as year ");
			gridsel.put("area", "ar.title as area ");
			gridsel.put("subject", "sub.title as subject ");
			gridsel.put("beginTime", ""+entity_alias_name+".beginTime as beginTime ");
			gridsel.put("endTime", ""+entity_alias_name+".endTime as endTime ");
			gridsel.put("status", "case when "+entity_alias_name+".status = 0 then '草稿' "
					+ " when "+entity_alias_name+".status = 1 then '待审核' "
					+ " when "+entity_alias_name+".status = 2 then '已发布'  end as status ");
			gridsel.put("valid", "case when "+entity_alias_name+".valid = 0 then '无效'"
					+ " when " +entity_alias_name+".valid = 1 then '有效' end  as valid ");
			List<Map<String, Object>> resultList = this.queryMapDataList(hql, gridsel,start , size);
			List<Map<String, Object>> festivalList = new ArrayList<Map<String,Object>>();
			if(null != resultList && resultList.size() > 0){
				for(Map map : resultList){
					if(null != map.get("beginTime")){
						String beginTime = df.format(map.get("beginTime"));
						map.put("beginTime", beginTime);
					}
					if(null != map.get("endTime")){
						String endTime = df.format(map.get("endTime"));
						map.put("endTime", endTime);
					}
					festivalList.add(map);
				}
			}
			return festivalList;
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<Map<String, Object>>();
		}
	}

	@Override
	public IFestivalDao getDao() throws Exception {
		return festivalDao;
	}

	@Override
	public List<Picture> getPictures(String id) {
		List<Picture> picList = new ArrayList();
		String sql = " select p from "+Picture.class.getName()+" p , "+Festival.class.getName()+" f "
				+ " where p in elements(f.relaPics) and f.id = :id order by p.id ";
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", id);
		try {
			picList = this.festivalDao.getCommonDaoService().queryList(sql, map);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return picList;
	}

	@Override
	public List<EnterpriseFestivalRela> getEntList(String id) {
		List<EnterpriseFestivalRela> entList = new ArrayList();
		String sql = " select efr from "+EnterpriseFestivalRela.class.getName()+" efr left outer join fetch efr.ent e left join efr.festival f "
				+ " where f.id = :id order by efr.id desc ";
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", id);
		try{
			entList = this.festivalDao.getCommonDaoService().queryList(sql, map);
		} catch (Exception e1){
			e1.printStackTrace();
		}
		return entList;
	}
	
	@Override
	public long findCountFestivalTwo() {
		String hql = "select count(t.id) FROM "+Festival.class.getName()+" as t "
				+ " WHERE t.status = 2 and t.valid = 1 ";
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -60);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
		hql = hql+" and STR_TO_DATE(t.subcribeTime,'%Y-%m-%d') >= '"+sdf.format(cal.getTime())+"'";
		return (long)this.festivalDao.getCommonDaoService().getByHQL(hql);
	}
	
	@Override
	public long findCountFestival() {
		String hql = "select count(t.id) FROM "+Festival.class.getName()+" as t "
				+ " WHERE t.status = 2 and t.valid = 1 ";
		return (long)this.festivalDao.getCommonDaoService().getByHQL(hql);
	}
	
	
	
	@Override
	public void deletePic(String picId,String festivalId) {
		this.festivalDao.deletePic(picId,festivalId);
	}



	@Autowired
	@Qualifier("daofestival")
	private IFestivalDao festivalDao;

	public IFestivalDao getFestivalDao() {
		return festivalDao;
	}
	public void setFestivalDao(IFestivalDao festivalDao) {
		this.festivalDao = festivalDao;
	}
}
